package com.licq.ldoc.domain;

/**
 * 
 * @author liujian
 *
 */
public final class Occurrencies {

	private static final int ONCE = 1;
	
	private static final int INFINIT_TIMES = -1;
	
	private static final String INTERVAL_MARK = "~";
	
	private static final String INFINIT_MARK = "*";
    
    public static final Occurrencies ANY = occur(0, INFINIT_TIMES);
    
    public static final Occurrencies ONLY_ONCE = occur(ONCE, ONCE);
    
    public static final Occurrencies ATLEAST_ONCE = least(ONCE);
    
    public static final Occurrencies ZERO_OR_ONCE = most(ONCE);
    
    private int least = 0;
    
    private int most = INFINIT_TIMES;
    
	private Occurrencies(int least, int most) {
		if (least > most && most != INFINIT_TIMES) {
			throw new IllegalArgumentException(String.format("argument least[%s] is bigger than most[%s].", 
					least, most
				)
			);
		}
		this.least = least;
		this.most = most;
	}
	
	public static Occurrencies occur(int least, int most) {
		return new Occurrencies(least, most);
	}
	
	public static Occurrencies least(int times) {
		return occur(times, INFINIT_TIMES);
	}
	
	public static Occurrencies most(int times) {
		return occur(0, times);
	}
	
	public static Occurrencies any() {
		return ANY;
	}
	
	@Override
	public Occurrencies clone() {
		Occurrencies cloned = new Occurrencies(least, most);
		return cloned;
	}

	public int getLeast() {
		return least;
	}

	public void setLeast(int least) {
		this.least = least;
	}

	public int getMost() {
		return most;
	}

	public void setMost(int most) {
		this.most = most;
	}

	public String toString() {
		if (least == most) {
			return String.valueOf(least);
		} else if (most == -1) {
			return new StringBuilder()
					.append(least)
					.append(INTERVAL_MARK)
					.append(INFINIT_MARK)
					.toString();
		} else {
			return new StringBuilder()
					.append(least)
					.append(INTERVAL_MARK)
					.append(most)
					.toString();
		}
	}

}
